# 版本说明:  2024.12.09 新建
# 制作人员:  dzy
# 功能说明： 组合一些json, 用于接口入参or?出参分析?
# 备注说明：
# 环境要求： python3/ windows32or64
# 设备要求：


# ---内置库---
import os
import sys
import time
import random

# ---第三方库---
from colorama import init, Fore, Back, Style
import pyperclip
import json

# ---自研库---
# from lib.mytools import getNowTime
# from lib.mytools import basicClass, readJsonFile
# from lib.Helper_Db_connector import Sqlite_Connector
from lib.mytools import getNowTime
from lib.mytools import basicClass, readJsonFile

# from Helper_Api_Test import By_Field

'''
比如工艺参数的组合


工艺参数code和工艺参数name ,还有参数值

构造json数据中的某个字段

工单号-> 工单bom
     -> 工单配方



解析data字段


构造一个 sap推送的iqc的检验数据


'''


class Para_Obj(basicClass):  # 一个参数对象, 不是一组!
    def __init__(self):
        super().__init__()
        self.instanceName = 'Para_Obj'
        self.param_code = ''
        self.param_name = ''
        self.act_value = ''
        self.material_batch_list = ''
        self.value_range = ''

    def to_dict(self):
        pass

    def from_dict(self):
        pass

    def __repr__(self):
        return f'<{self.instanceName}:remarks>'


class Api_Json_Maker(basicClass):
    """构造json, 解析json

    Args:
        basicClass (_type_): _description_
    """

    def __init__(self):
        super().__init__()
        self.instanceName = 'Api_Json_Maker'
        pass

    def to_param_one(self, param_obj):
        param_item = {
            "act_value": "150",
            "material_batch_list": [
                {
                    "material_batch_code": "30110FJB0004-12081655",
                    "material_num": "150"
                }
            ],
            "param_code": 'T1208-01',
            "param_name": '添加乙炔黑'
        }
        return param_item

    def to_json(self, param_list):
        # param_code param_name act_value material_batch_list(如果是物料参数的话)
        demo_json = {}
        param_list = []
        param_item = {
            "act_value": "150",
            "material_batch_list": [
                {
                    "material_batch_code": "30110FJB0004-12081655",
                    "material_num": "150"
                }
            ],
            "param_code": 'T1208-01',
            "param_name": '添加乙炔黑'
        }
        for item in param_list:
            print(item, type(item))
            demo_json['param_list'].append(item)

        demo_json = {
            "param_list": [  # 工艺参数
                {
                    "act_value": "150",
                    # "material_batch_list": [
                    #     {
                    #         "material_batch_code": "30110FJB0004-12081655",
                    #         "material_num": "150"
                    #     }
                    # ],
                    "param_code": 'T1208-01',
                    "param_name": '添加乙炔黑'
                },
                {
                    "act_value": "70",
                    # "material_batch_list": [
                    #     {
                    #         "material_batch_code": "",
                    #         "material_num": "70"
                    #     }
                    # ],
                    "param_code": 'T1208-02',
                    "param_name": '去离子水添加'
                },
                {
                    "act_value": "201",
                    "material_batch_list": [
                        {
                            "material_batch_code": "30110FJB0004-12081655",
                            "material_num": "201"
                        }
                    ],
                    "param_code": 'T1208-03',
                    "param_name": '乙炔黑代替石墨'  # 虽然参数上写的石墨,但是投料是用乙炔黑,因为石墨没有在bom中
                },
            ],
        }

    def parse_json(self):  # 解析json 提取出需要的字段
        {
            'dataSource': 'CM1',
            'factoryCode': 'W1',
            'factoryName': 'W1',
            'processSegmentCode': 'W1CC01',
            'processSegmentName': 'CM1-阴极',
            'workorderNo': '1000000658',  # 工单号
            'productBatchNo': 'CACJB241205D0101',
            'productName': 'CQ阴极极片',
            'productCode': '201501030001',
            'processCode': 'CJB',
            'unit': 'EA',
            'productDate': '2024-12-05 00:00:00',
            'actualQty': 0,
            'status': 0,
            'qualityStatus': 1,
            'preProcessCode': None
        }

    def get_workorder_id(self, fieldName, dict1: dict):  # TODO
        # 通过莫某字段来 , 获取工单号
        choice = fieldName
        match choice:
            case By_Field.process_id:
                msg = f'通过  {fieldName} 某种方式来获取哦工单号'
                self.classPrint(msg)

                ...
            case By_Field.batch_id:
                print('跳出循环')
            case _:
                msg = '啥都没有匹配到'
                self.classPrint(Fore.RED + msg)
                return False, msg

    def read_json(self, jsonFile):
        #
        return readJsonFile(jsonFile)

    def random_fluctuation_normal(self, value, fluctuation_range):
        """
        输入一个数值和波动范围，返回一个随机波动后的数值，波动符合正态分布并保留三位有效小数。

        :param value: 基础数值
        :param fluctuation_range: 波动范围，用于控制标准差
        :return: 波动后的随机数值，符合正态分布并保留三位有效小数
        """
        # 将波动范围作为标准差（sigma）
        # 假设均值mu为0，波动范围控制标准差
        # 波动范围内大部分值会落在[-fluctuation_range, fluctuation_range]之间
        sigma = fluctuation_range / 3
        fluctuation = random.normalvariate(0, sigma)  # 生成符合正态分布的波动值
        result = value + fluctuation
        return round(result, 3)
